//
//  NSDate+Formater.m
//  VoiceMemo
//
//  Created by aojun on 15/12/30.
//  Copyright © 2015年 aojun. All rights reserved.
//

#import "NSDate+Formater.h"




@implementation NSDate (Formater)

///返回UNIX时间戳
+ (NSString *)UNIXDate{
    
    NSDate* dat = [NSDate dateWithTimeIntervalSinceNow:0];
    NSTimeInterval a=[dat timeIntervalSince1970];
    //转为字符型取整数部分
    NSString *timeString = [NSString stringWithFormat:@"%.0f", a];
    return timeString;
}


+ (UInt32)IntUINXDate{
    NSDate* dat = [NSDate dateWithTimeIntervalSinceNow:0];
    NSTimeInterval a =[dat timeIntervalSince1970];
    return a;
}

// 获取今天的日期
+ (NSString *)getCurrentTimeWithFormat:(NSString *)format{
    
    // 时间格式器
    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
    [formatter setDateFormat:format];
    
    // 当前时间
    NSDate *date = [NSDate date];
    // 获取指定格式的时间
    NSString *formatString = [formatter stringFromDate:date];
    
    return formatString;
}

// 获取指定格式的日期
+ (NSString *)getDateInterval:(int)interval Format:(NSString *)format{
    // 时间格式器
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    [dateFormatter setDateFormat:format];
    // 当前时间
    NSDate *date = [NSDate date];
    // 后N天
    NSDate *resultDate = [NSDate dateWithTimeInterval:24*60*60*interval sinceDate:date];
    // 获取对应的时间
    NSString *result = [dateFormatter stringFromDate:resultDate];
    return result;
}

// 获取后几天的年月日 指定格式
+ (NSString *)getBaseDate:(NSDate *)baseDate Interval:(int)interval Format:(NSString *)format{
    // 时间格式器
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    [dateFormatter setDateFormat:format];
    // 后N天
    NSDate *resultDate = [NSDate dateWithTimeInterval:24*60*60*interval sinceDate:baseDate];
    // 获取对应的时间
    NSString *result = [dateFormatter stringFromDate:resultDate];
    return result;
}

// 获取后几天是周几
+ (NSArray *)getWeekInterval:(int)interval Format:(NSString *)format{
    // 时间格式器
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    [dateFormatter setDateFormat:format];
    // 当前时间
    NSDate *date = [NSDate date];
    // 后N天
    NSDate *resultDate = [NSDate dateWithTimeInterval:24*60*60*interval sinceDate:date];
    // 获取对应的时间
    NSString *result = [dateFormatter stringFromDate:resultDate];
    
    // 获取对应的周几
    
    NSDateComponents *componets = [[NSCalendar autoupdatingCurrentCalendar] components:NSCalendarUnitWeekday fromDate:resultDate];
    //a就是星期几，1代表星期日，2代表星期一，后面依次
    NSInteger weekday = [componets weekday] - 1;
    // 让星期一一对应上
    weekday = weekday == 0 ? 7 : weekday;
    
    return @[[NSNumber numberWithInteger:weekday],result];
}

// 获取指定周几
+ (NSString *)getWeekWithTimeString:(NSString *)string Format:(NSString *)format{
    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
    [formatter setDateFormat:format];
    
    NSDate *date = [formatter dateFromString:string];
    
    [formatter setDateFormat:@"EEEE"];
    NSString *week = [formatter stringFromDate:date];
    
    return week;
}


// 时间戳转时间  常用
+ (NSString *)getDateWithUnixDate:(NSString *)udate Formatter:(NSString *)format{
    NSDate *confromTimesp = [NSDate dateWithTimeIntervalSince1970:[udate doubleValue]];
    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
    [formatter setDateFormat:format];
    NSString *result = [formatter stringFromDate:confromTimesp];
    return result;
}

// 将时间转换成指定格式
+ (NSString *)getFormatterDateWithDate:(NSString *)date
                         DateFormatter:(NSString *)dateFormat
                             Formatter:(NSString *)format{
    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
    [formatter setDateFormat:dateFormat];
    NSDate *rdate = [formatter dateFromString:date];
    [formatter setDateFormat:format];
    NSString *result = [formatter stringFromDate:rdate];
    
    return result;
}

// 将时间转换成指定格式
+ (NSDate *)getDateWithDate:(NSString *)date
              DateFormatter:(NSString *)dateFormat{
    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
    [formatter setDateFormat:dateFormat];
    NSDate *rdate = [formatter dateFromString:date];
    
    return rdate;
}


//时间转换 (多少天前，多少小时前 返回NSString)
+ (NSString *)timeWithTimeIntervalString:(NSString *)timeString
{
    // 格式化时间
    NSDateFormatter* formatter = [[NSDateFormatter alloc] init];
    formatter.timeZone = [NSTimeZone localTimeZone];
    [formatter setDateStyle:NSDateFormatterMediumStyle];
    [formatter setTimeStyle:NSDateFormatterShortStyle];
    [formatter setDateFormat:@"MM-dd HH:MM"];
    // 毫秒值转化为秒
    NSDate* date = [NSDate dateWithTimeIntervalSince1970:[timeString doubleValue]];
    NSString* dateString = [formatter stringFromDate:date];
    
    // 两个时间戳的间隔   返回的是秒
    NSDate *currentDate = [NSDate date];
    NSTimeInterval time = [currentDate timeIntervalSinceDate:date];
    NSString *mydateStr = [NSString stringWithFormat:@"%.0f",time];
    int mydate = [mydateStr intValue];
    if (mydate / (3600 * 24)  <= 0) {  //一天内
        
        if (mydate / 3600 <= 0) {
            dateString = [NSString stringWithFormat:@"%d分钟前",mydate / 60];
        }else{
            dateString = [NSString stringWithFormat:@"%d小时前",mydate / 3600];
        }
    }else{
        dateString = [NSString stringWithFormat:@"%d天前",mydate / (3600 * 24)];
    }
    NSLog(@"time   %f   %f",time,time / 3600 / 24);
    
    
    return dateString;
}

+ (NSString *)timeCutDownIntervalString:(NSString *)timeString{
    
    
    // 格式化时间
    NSDateFormatter* formatter = [[NSDateFormatter alloc] init];
    formatter.timeZone = [NSTimeZone localTimeZone];
    [formatter setDateStyle:NSDateFormatterMediumStyle];
    [formatter setTimeStyle:NSDateFormatterShortStyle];
    [formatter setDateFormat:@"MM-dd HH:MM"];
    // 毫秒值转化为秒
    NSDate* date = [NSDate dateWithTimeIntervalSince1970:[timeString doubleValue]];
    NSString* dateString = [formatter stringFromDate:date];
    
    // 两个时间戳的间隔   返回的是秒
    NSDate *currentDate = [NSDate date];
    NSTimeInterval time = [currentDate timeIntervalSinceDate:date];
    NSString *mydateStr = [NSString stringWithFormat:@"%.0f",time];
    int mydate = [mydateStr intValue];
    if (mydate / (3600 * 24)  <= 0) {  //一天内
        
        if (mydate / 3600 <= 0) {
            dateString = [NSString stringWithFormat:@"%d分钟",mydate / 60];
        }else{
            dateString = [NSString stringWithFormat:@"%d小时",mydate / 3600];
        }
    }else{
        dateString = [NSString stringWithFormat:@"%d天",mydate / (3600 * 24)];
    }
    return dateString;
    
}


+ (NSString *)timeWithTimeStampString:(NSString *)timeString{
    // 格式化时间
    NSDateFormatter* formatter = [[NSDateFormatter alloc] init];
    formatter.timeZone = [NSTimeZone localTimeZone];
    [formatter setDateStyle:NSDateFormatterMediumStyle];
    [formatter setTimeStyle:NSDateFormatterShortStyle];
    [formatter setDateFormat:@"MM-dd HH:MM"];
    // 毫秒值转化为秒
    NSDate* date = [NSDate dateWithTimeIntervalSince1970:[timeString doubleValue]];
    NSString* dateString = [formatter stringFromDate:date];
    return dateString;
}

+ (NSString *)timeWihYYMMDDString:(NSString *)timeString{
    // 格式化时间
    NSDateFormatter* formatter = [[NSDateFormatter alloc] init];
    formatter.timeZone = [NSTimeZone localTimeZone];
    [formatter setDateStyle:NSDateFormatterMediumStyle];
    [formatter setTimeStyle:NSDateFormatterShortStyle];
    [formatter setDateFormat:@"YYYY-MM-dd"];
    // 毫秒值转化为秒
    NSDate* date = [NSDate dateWithTimeIntervalSince1970:[timeString doubleValue]];
    NSString* dateString = [formatter stringFromDate:date];
    return dateString;
}


+ (NSString *)formattedTimeWithUtime:(NSString *)utime Format:(NSString *)format {
    
    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
    [formatter setDateFormat:format];
    NSDate *confromTimesp = [NSDate dateWithTimeIntervalSince1970:[utime doubleValue]];
    NSString *confromTimespStr = [formatter stringFromDate:confromTimesp];
    
    //时间戳转时间的方法:
    NSDate *date = [formatter dateFromString:confromTimespStr];
    NSString *res = [formatter stringFromDate:date];
    
    return res;
}


// 计算相差时间
+ (int)differTwoTime:(NSString *)utime1 Time2:(NSString *)utime2{
    // 时间差
    double timediff = fabsl([utime1 doubleValue]-[utime2 doubleValue]);
    int iHours = timediff/3600;
    return iHours;
}

+ (BOOL)timeIstoday:(NSString *)utime{
    NSString *timeDay = [NSDate formattedTimeWithUtime:utime Format:@"yyyyMMdd"];
    NSString *today = [NSDate formattedTimeWithUtime:[NSDate UNIXDate] Format:@"yyyyMMdd"];
    BOOL res = [timeDay doubleValue] == [today doubleValue];
    //    BOOL res = [timeDay isEqualToString:today];
    return res;
}


@end
